from django.shortcuts import render

# Create your views here.
from rest_framework import viewsets
from rest_framework.decorators import action
from rest_framework.permissions import IsAuthenticated, AllowAny
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.viewsets import ViewSet, ModelViewSet, GenericViewSet

from app_student.models import Teacher, Student
from app_student.serializers import TeacherSerializer, StudentSerializer


# 前端3  请求，  资源标签，  form表单，  ajax
# ajax，   认证， 权限，  限流，

class StudentModelViewSet(ModelViewSet):
    queryset = Student.objects.all()
    serializer_class = StudentSerializer


class TeacherAPIView(APIView):
    # get ->  get
    def get(self, request, pk):
        teacher = Teacher.objects.get(id=pk)
        return Response(teacher.values())

    def put(self, request, pk):
        pass

    def delete(self, request, pk):
        pass


class TeacherListCreateAPIView(APIView):
    def post(self, request):
        pass

    def put(self, request):
        pass


class TeacherViewSet(ViewSet):
    queryset = Teacher.objects.all()

    def create(self, request):
        pass

    def update(self, request, pk):
        pass

    def destroy(self, request, pk):
        pass

    def setunact(self, request, pk):
        # self.get_queryset
        t = Teacher.objects.get(id=pk)
        t.active = False
        t.save()
        return Response({'id': t.id, 'tname': t.tname})

    def retrieve(self, request, pk):
        pass

    def list(self, request):
        pass

    @action(methods=['get'], detail=False)  # /teacher/unactive/   /teacher/pk/unactive/
    def unactive(self, request):
        # 获取不上岗的老师
        ts = Teacher.objects.filter(active=False)
        return Response(ts.values())


class TeacherGenericViewSet(GenericViewSet):
    queryset = Teacher.objects.all()
    serializer_class = TeacherSerializer

    # get_object、get_queryset
    @action(methods=['get'], detail=True)
    def setunact(self, request):
        # t = Teacher.objects.get(id=pk)
        t = self.get_object()
        t.active = False
        t.save()
        return Response({'id': t.id, 'tname': t.tname})

    @action(methods=['get'], detail=False)
    def unactive(self, request):
        # 获取不上岗的老师
        # ts = Teacher.objects.filter(active=False)
        ts = self.get_queryset().filter(active=False)
        return Response(ts.values())


# class TeacherModelViewSet(ModelViewSet):
#     queryset = Teacher.objects.all()
#     serializer_class = TeacherSerializer
#
#     @action(methods=['get'], detail=True)
#     def setunact(self, request):
#         # t = Teacher.objects.get(id=pk)
#         t = self.get_object()
#         t.active = False
#         t.save()
#         return Response({'id': t.id, 'tname': t.tname})
#
#     @action(methods=['get'], detail=False)
#     def unactive(self, request):
#         # 获取不上岗的老师
#         ts = Teacher.objects.filter(active=False)
#         return Response(ts.values())

class TeacherModelViewSet(ModelViewSet):
    queryset = Teacher.objects.all()
    serializer_class = TeacherSerializer
